﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zor_DAL;
using Zor_Modes;
using Zor_Tools;

namespace Zor_Bll
{
    public class Zor_RepealJinSan_Bll<T> where T : class
    {
        /// 获取金三销号信息列表
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="TotalCount">返回值</param>
        /// <returns></returns>
        public static List<T> GetPageList(dynamic Model,int index, out int TotalCount, string where)
        {
            int Dtid = RequestSession.GetSessionUser().DT_Id; //当前登录人的部门ID
            int Isadmin = RequestSession.GetSessionUser().Is_Admin; //当前登录人是否为管理员
            var sqlSurch = " 1=1 ";
            if (Model.CreateStartTime == null &&
                Model.CreateEndTime == null &&
                Model.Acceptance_TicketNumber == null &&
                Model.NatPer_Name == null &&
                Model.Tenant_Name == null &&
                Model.Js_Zigui == null &&
                Model.JsState == null
                )
            {
                if (Isadmin == 1)
                {
                    sqlSurch = " 1=1 ";    //管理员登录显示所有
                }
                else
                {
                    sqlSurch = " Sws_Id = " + Dtid + "  and JsState ='发票已出' ";
                }
            }
            else
            {
                if (Isadmin == 1)
                {
                    sqlSurch = " 1=1 ";    //管理员登录显示所有
                }
                else
                {
                    sqlSurch = " Sws_Id = " + Dtid + " ";
                }
                if (Model.CreateStartTime != null && Model.CreateEndTime == null)
                {
                    int StartTime = TimeHelper.ToTimeStamp(DateTime.Parse(Model.CreateStartTime.ToString()));
                    sqlSurch += " and " + "Js_CreateDate = " + StartTime + " ";
                }
                if (Model.CreateEndTime != null && Model.CreateStartTime == null)
                {
                    int EndTime = TimeHelper.ToTimeStamp(DateTime.Parse(Model.CreateEndTime.ToString()));
                    sqlSurch += " and " + "Js_CreateDate = " + EndTime + " ";
                }
                if (Model.CreateStartTime != null && Model.CreateEndTime != null)
                {
                    int StartTime = TimeHelper.ToTimeStamp(DateTime.Parse(Model.CreateStartTime.ToString()));
                    int EndTime = TimeHelper.ToTimeStamp(DateTime.Parse(Model.CreateEndTime.ToString()));

                    sqlSurch += " and " + "Js_CreateDate between " + StartTime + "  and " + EndTime + "";
                }
                if (Model.Acceptance_TicketNumber != null)
                {
                    sqlSurch += " and " + "Acceptance_TicketNumber = '" + Model.Acceptance_TicketNumber + " '";
                }
                if (Model.NatPer_Name != null)
                {
                    sqlSurch += " and " + " NatPer_Name like '%" + Model.NatPer_Name + "%' ";
                }
                if (Model.Tenant_Name != null)
                {
                    sqlSurch += " and " + " Tenant_Name like '%" + Model.Tenant_Name + "%'  ";
                }
                if (Model.Js_Zigui != null)
                {
                    sqlSurch += " and " + " Js_Zigui like '%" + Model.Js_Zigui + "%'  ";
                }
                if (Model.JsState != null)
                {
                    if (Model.JsState == "全部")
                    {
                        sqlSurch += " and " + "JsState in ('已保存','发票已出','已销号')";
                    }
                    else
                    {
                        sqlSurch += " and " + "JsState = '" + Model.JsState + " '";
                    }
                }          
 
            }

           
            return PagerHelp<T>.GetPagerList(index, "V_Zor_RepealJinSan", "Js_Id", out TotalCount, sqlSurch);
        }

        /// <summary>
        /// 金三销号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static BaseAnswer RepealJinSan(int Jsid, int AcceptanceId)
        {
            BaseAnswer bar = Zor_RepealJinSan_DAL<T>.RepealJinSan(Jsid, AcceptanceId);
            if (bar.AnswerType != BaseAnswer.Answers.Error)
                bar.AnswerMeg = bar.AnswerType == BaseAnswer.Answers.Succee ? "金三销号成功！" : "金三销号失败，请重试！";
            return bar;
        }
    }
}
